//题库
let titles = []
//错题数组
let errorOptions = []
Page({
   //一进入页面就进入生命周期
   data: {
     userSelect: '' ,
     userScore: 0,
     totalScore:-1, //总分
     totalError:0,
      percent: 0,
      total:0,
      isSelect: false,
      subject: null,
      current:1,     //从1开始
    
      
   },

   onLoad:function(options){
      var a=options.a
    console.log('a',a)
    if(a==1){
      wx.cloud.database().collection('questions').where({"type":"马一"}).get().then(res =>{
       console.log('题库数据',res)
       titles=res.data
       let subject = titles[0]
     console.log('subject',subject)
     this.setData({
       subject,
       total:titles.length   // get总题目
     })
     })}
    else if(a==2){
      wx.cloud.database().collection('questions').where({"type":"马二"}).get().then(res =>{
      titles=res.data
      let subject = titles[0]
    this.setData({
      subject,
      total:titles.length   // get总题目
    })
    })}
    else if(a==3){
      wx.cloud.database().collection('questions').where({"type":"马三"}).get().then(res =>{
      titles=res.data
      let subject = titles[0]
    this.setData({
      subject,
      total:titles.length   // get总题目
    })
    })}
    else if(a==4){
      wx.cloud.database().collection('questions').where({"type":"马四"}).get().then(res =>{
      titles=res.data
      let subject = titles[0]
    this.setData({
      subject,
      total:titles.length   // get总题目
    })
    })}
    else if(a==31){
      wx.cloud.database().collection('questions2').where({"type":"思一"}).get().then(res =>{
      titles=res.data
      let subject = titles[0]
    this.setData({
      subject,
      total:titles.length   // get总题目
    })
    })}
    else if(a==21){
      wx.cloud.database().collection('questions3').get().then(res =>{
      titles=res.data
      let subject = titles[0]
    this.setData({
      subject,
      total:titles.length   // get总题目
    })
    })}
    else {
      wx.cloud.database().collection('questions4').where({"type":"毛一"}).get().then(res =>{
      titles=res.data
      let subject = titles[0]
    this.setData({
      subject,
      total:titles.length   // get总题目
    })
    })}
    
     
   },
   //用户选择
   radioChange(e){
     console.log(e.detail.value)
     this.setData({
       userSelect: e.detail.value
     })
   },

   //提交答题跳转下一题
   submit(){
    let userSelect=this.data.userSelect //获取用户选项
    if(!userSelect){
      wx.showToast({
        icon: 'none', 
        title: '请做出选择',
      })
      return
    }//判定是否为空

    console.log('用户选项',userSelect)
    let currentNum= this.data.current
     //更新进度条
     this.setData({
      percent: ((currentNum/titles.length)*100).toFixed(2)
     })
     
  //判断选择是否正确
  console.log('正确答案是',this.data.subject.answer)
  console.log('用户选择了',userSelect)
  if (this.data.subject.answer.indexOf(userSelect)> -1){
    console.log('用户答对了第'+currentNum+'道题')
    this.setData({
      userScore: this.data.userScore +1
    })
  }else {
    //错题集
    let subjectNow = this. data.subject
    subjectNow.userSelect = userSelect
    console.log('错题',subjectNow)
    errorOptions.push(subjectNow)
  }
  console.log('用户共答对了'+this.data.userScore + '道题')


  //判断是否是最后一题,并打分
      if (currentNum + 1 > titles.length) {
        let totalScore = (this.data.userScore/titles.length*100).toFixed(1)
        console.log('用户总得分为:',totalScore)
        console.log('用户错题为:',errorOptions)
        this.setData({
          totalScore:totalScore,
          totalError:errorOptions.length
        })
        wx.showToast({
          icon: 'none',
          title: '已经是最后一题',
        })
        return
      }

     let subject = titles[currentNum]


     this.setData({
       userSelect: '',
       subject,
       current:currentNum +1,
       isSelect: false,
       
     })
   },
   //查看错题集
   seeError(){
     console.log('点击了查看错题集')
     //存数据
     wx.setStorageSync('xdd', errorOptions)

     wx.navigateTo({
       url: '/pages/errorOptions/errorOptions',
     })
   }
  
})